package com.baizhi.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.baizhi.config.OSSConfig;

import java.util.Map;

public class AliyunSms {

    public static Map<String,String> sms(String phone,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", OSSConfig.ACCESS_KEY_ID, OSSConfig.ACCESS_KEY_SECRET);//自己账号的AccessKey信息
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//短信服务的服务接入地址
        request.setSysVersion("2017-05-25");//API的版本号
        request.setSysAction("SendSms");//API的名称
        request.putQueryParameter("PhoneNumbers", phone);//接收短信的手机号码
        request.putQueryParameter("SignName", "登录验证");//短信签名名称
        request.putQueryParameter("TemplateCode", "SMS_4020641");//短信模板ID
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\",\"product\":\"[应学]\"}");//短信模板变量对应的实际值
        //                                  {\"code\":\"1234\","product":\"[应学]\"}
        Map<String,String> map = null;
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());  //转成对象
            map = JSON.parseObject(response.getData(), Map.class);
        } catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }
}
